package com.xiangyu.demo.service;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.xiangyu.demo.bean.UserInfo;
import com.xiangyu.demo.configuration.jms.Producer;
import com.xiangyu.demo.dao.DemoMapper;
import com.xiangyu.demo.utils.RedisUtil;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Topic;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

/**
 * java类简单作用描述
 *
 * @ProjectName: springboot-demo
 * @Package: com.xiangyu.demo.service
 * @Description: java类作用描述
 * @Author: 向羽
 * @CreateDate: 2018/1/10 18:52
 * @UpdateUser: 向羽
 * @UpdateDate: 2018/1/10 18:52
 * @UpdateRemark: The modified content
 * @Version: 2.0
 */
@Service
public class DemoService {

    @Autowired
    private DemoMapper demoMapper;
    @Autowired
    private RedisUtil redisUtil;
//    @Resource(name = "transactionManager")
    @Autowired
    private DataSourceTransactionManager transactionManager;
    @Autowired
    private Producer producer;
    @Autowired
    private Topic topic;
    @Autowired
    private Queue queue;

    // value属性指定Cache名称
    // 使用key属性自定义key
    // condition属性指定发生的条件(如上示例表示只有当user的id为偶数时才会进行缓存)
//    @Cacheable(value = "userInfo", key = "#root.target+ '_' + #root.methodName +'_' + #userInfo.organId+#pageBounds.limit+#pageBounds.page")
    public List<UserInfo> getHello(UserInfo userInfo,PageBounds pageBounds){
        List<UserInfo> userInfoList = demoMapper.getUserList(userInfo,pageBounds);
        System.out.println(userInfoList.toString());
//        redisUtil.set2("111",userInfoList);
        try {
//            Destination destination = new ActiveMQQueue("mytest.queue");
            for (UserInfo user:userInfoList) {
                producer.sendStringMessage(queue,user.toString());
                producer.sendObjectMessage(topic,user);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userInfoList;
    }

    @Cacheable(value = "userInfo", key = "'findUser' + #map.organId")
    public List<UserInfo> getHello2(UserInfo userInfo){
        PageBounds pageBounds = new PageBounds(1,10);
        List<UserInfo> userInfoList = demoMapper.getUserList(userInfo,pageBounds);
        System.out.println(userInfoList.toString());
        redisUtil.set2("222",userInfoList);
        return userInfoList;
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "userInfo",allEntries = true )
    public boolean delUser(UserInfo userInfo){
        DefaultTransactionDefinition transDefinition = new DefaultTransactionDefinition();
        //开启新事物
        transDefinition.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRES_NEW);
        TransactionStatus transStatus = transactionManager.getTransaction(transDefinition);
        boolean delNum = demoMapper.delUser(userInfo)>0;
        try {

            transactionManager.commit(transStatus);
        } catch (TransactionException e) {
            transactionManager.rollback(transStatus);
            e.printStackTrace();
        }
        return delNum;
    }

    public static void main(String[] args) {
//        String s = null;
//        System.out.println(Optional.ofNullable(s).map(String::length).orElse(0));
        Optional<String> optional = Optional.empty();
        System.out.println(optional.orElse(""));
    }
}
